﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class UnitDao
    {
        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT * FROM [dbo].[Unit]";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Unit";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, UnitData data)
        {
            string commandText = "Update [dbo].[Unit] SET UnitName = @UnitName, TelNo = @TelNo, FaxNo = @FaxNo, PostNo = @PostNo, Address = @Address, Url = @Url, Email = @Email, BankName = @BankName, BankNo = @BankNo";
            SqlParameter[] commandParameters = new SqlParameter[9];
            commandParameters[0] = new SqlParameter("@UnitName", SqlDbType.NVarChar, 100);
            if (data.UnitNameIsNull)
            {
                commandParameters[0].Value = DBNull.Value;
            }
            else
            {
                commandParameters[0].Value = data.UnitName;
            }
            commandParameters[1] = new SqlParameter("@TelNo", SqlDbType.NVarChar, 100);
            if (data.TelNoIsNull)
            {
                commandParameters[1].Value = DBNull.Value;
            }
            else
            {
                commandParameters[1].Value = data.TelNo;
            }
            commandParameters[2] = new SqlParameter("@FaxNo", SqlDbType.NVarChar, 100);
            if (data.FaxNoIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.FaxNo;
            }
            commandParameters[3] = new SqlParameter("@PostNo", SqlDbType.NVarChar, 50);
            if (data.PostNoIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.PostNo;
            }
            commandParameters[4] = new SqlParameter("@Address", SqlDbType.NVarChar, 100);
            if (data.AddressIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.Address;
            }
            commandParameters[5] = new SqlParameter("@Url", SqlDbType.NVarChar, 100);
            if (data.UrlIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.Url;
            }
            commandParameters[6] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            if (data.EmailIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.Email;
            }
            commandParameters[7] = new SqlParameter("@BankName", SqlDbType.NVarChar, 100);
            if (data.BankNameIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.BankName;
            }
            commandParameters[8] = new SqlParameter("@BankNo", SqlDbType.NVarChar, 100);
            if (data.BankNoIsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.BankNo;
            }
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }
    }
}

